//
//  ComposeController.m
//  MyNewWeiBo
//
//  Created by wpapple on 16/5/30.
//  Copyright © 2016年 heron. All rights reserved.
//

#import "ComposeController.h"
#import "BottomView.h"
#import "Account.h"
#import "ImageCollectionViewController.h"

@interface ComposeController ()<UITextViewDelegate,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (nonatomic, strong) BottomView * btmView;
@property (nonatomic, strong) ImageCollectionViewController * imageCVC;

@end

@implementation ComposeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view bringSubviewToFront:self.textLabel];
    self.textView.delegate = self;
    self.textView.alwaysBounceVertical = YES;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    BottomView * btmView = [[BottomView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 44, [UIScreen mainScreen].bounds.size.width, 44)];
    self.btmView = btmView;
    [self.view addSubview:btmView];
    
}
- (IBAction)sendInfo:(UIBarButtonItem *)sender {
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    Account * account = [Account accountFromSandbox];
    NSMutableDictionary * parameters = [NSMutableDictionary dictionary];
    [parameters setValue:account.access_token forKey:@"access_token"];
    [parameters setValue:self.textView.text forKey:@"status"];
    if (self.imageCVC.imageArray.count == 0) {
        NSString * urlStr = @"https://api.weibo.com/2/statuses/update.json";
        
        [manager POST:urlStr parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"发送成功");
            [self dismissViewControllerAnimated:YES completion:nil];
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"发送失败");
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }else{
        NSString * urlStr = @"https://upload.api.weibo.com/2/statuses/upload.json";
        
        [manager POST:urlStr parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            UIImage * image = [self.imageCVC.imageArray firstObject];
            NSData * imageData = UIImagePNGRepresentation(image);
            [formData appendPartWithFileData:imageData name:@"pic" fileName:@"pic.png" mimeType:@"image/png"];
        } success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"发送成功");
            [self dismissViewControllerAnimated:YES completion:nil];
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"发送失败");
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }
    
    
    
}



- (IBAction)close:(UIBarButtonItem *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}


- (void)keyboardChange:(NSNotification *)notification{
    NSLog(@"%@",notification);
    CGRect rect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
    CGFloat y = rect.origin.y;
    
    CGFloat finalY = y - 44;
    
    CGFloat time = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    [UIView animateWithDuration:time animations:^{
        self.btmView.y = finalY;
    }];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)textViewDidChange:(UITextView *)textView{
    if (self.textView.text.length > 0) {
        self.textLabel.text = @"";
    }else{
        self.textLabel.text = @"写些什么吧";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    ImageCollectionViewController * imageCVC = segue.destinationViewController;
    self.imageCVC = imageCVC;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
